package com.one.common.view.pagestate.listpage;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.one.common.R;
import com.one.common.presenter.BasePresenter;
import com.one.common.view.multitytype.ItemViewBinder;
import com.one.common.view.multitytype.MultiTypeAdapter;
import com.one.common.view.pagestate.refreshlayout.IHeaderView;
import com.one.common.view.pagestate.refreshlayout.TwinklingRefreshLayout;
import com.one.common.view.pagestate.statepage.BaseStateFragment;

import java.util.List;


/**
 * creator: zhulunjun
 * time:    2018/3/8
 * describe: 带状态的列表页面
 */

public abstract class BaseListFragment<T extends BasePresenter> extends BaseStateFragment<T> implements ListPageInterface {

    private ListPageHandle mListPageHandle;

    @Override
    public int getLayoutResId() {
        return R.layout.page_list;
    }

    @Override
    public void initView() {
        super.initView();
        mListPageHandle = new ListPageHandle(getActivity(), successView, this, this);
        mListPageHandle.initView();
    }

    @Override
    public void loading() {
        super.loading();
        loadData();
    }

    @Override
    public void refresh() {
        // 主动刷新
        mListPageHandle.refresh();
    }

    @Override
    public void loadMore() {
        // 主动加载更多
        mListPageHandle.loadMore();
    }

    @Override
    public void doRefresh() {
        mListPageHandle.doRefresh();
    }

    @Override
    public void doLoadMore() {
        mListPageHandle.doLoadMore();
    }

    @Override
    public void loadSuccess(List<?> data) {
        mListPageHandle.loadSuccess(data);
    }

    @Override
    public void loadFail(String errorMessage) {
        mListPageHandle.loadFail(errorMessage);
    }


    @Override
    public void replaceData(List<?> data) {
        mListPageHandle.replaceData(data);
    }

    /**
     * 注册数据
     *
     * @param clazz  数据模型
     * @param binder 绑定视图的适配器
     */
    public <T> void register(@NonNull Class<? extends T> clazz, @NonNull ItemViewBinder<T, ?> binder) {
        mListPageHandle.register(clazz, binder);
    }

    @Override
    public void hindLoadMore() {
        mListPageHandle.hindLoadMore();
    }

    @Override
    public void hindRefresh() {
        mListPageHandle.hindRefresh();
    }

    @Override
    public void showLoadMore() {
        mListPageHandle.showLoadMore();
    }

    @Override
    public void showRefresh() {
        mListPageHandle.showRefresh();
    }

    @Override
    public void addBottomView(int resId) {
        mListPageHandle.addBottomView(resId);
    }

    @Override
    public void addTopView(int resId) {
        mListPageHandle.addTopView(resId);
    }

    @Override
    public void addFloatTopView(int resId) {
        mListPageHandle.addFloatTopView(resId);
    }

    @Override
    public void addData(Object o) {
        mListPageHandle.addData(o);
    }

    @Override
    public void addData(int index, Object o) {
        mListPageHandle.addData(index,o);
    }
    @Override
    public void deleteData(Object o) {
        mListPageHandle.deleteData(o);
    }

    @Override
    public void deleteData(int index) {
        mListPageHandle.deleteData(index);
    }

    @Override
    public boolean getStackFromEnd() {
        return false;
    }

    @Override
    public List<?> getData() {
        return mListPageHandle.getData();
    }

    @Override
    public RecyclerView getListView() {
        return mListPageHandle.getListView();
    }

    @Override
    public int getPage() {
        return mListPageHandle.getPage();
    }

    @Override
    public MultiTypeAdapter getAdapter() {
        return mListPageHandle.getAdapter();
    }

    @Override
    public RecyclerView.LayoutManager getLayoutManager() {
        return null;
    }

    @Override
    public void setHeadView(IHeaderView headerView) {
        mListPageHandle.setHeadView(headerView);
    }

    @Override
    public void setHeaderColor(int color) {
        mListPageHandle.setHeaderColor(color);
    }

    @Override
    public TwinklingRefreshLayout getRefresh() {
        return mListPageHandle.getRefresh();
    }
}
